package com.adams.access.db.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * 
 * 
 * File Name : DicColumn.java
 * 
 * @Description : 字段定义的基本信息 实体
 */
@Entity
@Table(name = "T_COLUMN_INFO")
public class DicColumn implements Serializable
{

	private static final long serialVersionUID = 8410519167922090084L;

	private DicColumnPK dicColumnPK;// 复合主键

	private String columnname;// 字段名

	private String datatype;// 数据类型
	// S—字符型、N—数字型、D—日期型、T—时间型、L—逻辑型、U-URL资源、UI-URL图片资源
	public static String DATATYPE_STR = "S";
	public static String DATATYPE_NUM = "N";
	public static String DATATYPE_DATE = "D";
	public static String DATATYPE_TIME = "T";
	public static String DATATYPE_LOGIC = "L";
	public static String DATATYPE_URL = "U";
	public static String DATATYPE_URL_IMG = "UI";

	public static Set<String> STR_DATATYPE_SET = new HashSet<String>();
	static
	{
		STR_DATATYPE_SET.add(DATATYPE_STR);
		STR_DATATYPE_SET.add(DATATYPE_DATE);
		STR_DATATYPE_SET.add(DATATYPE_TIME);
		STR_DATATYPE_SET.add(DATATYPE_URL);
		STR_DATATYPE_SET.add(DATATYPE_URL_IMG);
	}

	private String enumeratemap;// key:value 枚举串 ;分隔
	public static String ENUMERATE_SEP = ";";// key:value 枚举串 分隔符
	public static String ENUMERATE_KEY_TAG = "key";
	public static String ENUMERATE_VALUE_TAG = "value";
	public static List<String> ENUMERATE_TAG_SET = new ArrayList<String>();
	static
	{
		ENUMERATE_TAG_SET.add(ENUMERATE_KEY_TAG);
		ENUMERATE_TAG_SET.add(ENUMERATE_VALUE_TAG);
	}
	private String sourceelement;// 来源数据项

	private String sourcetable;// 来源数据表，是来源数据项实际对应表

	private String targettable;// 目标数据表，主要应用于多对多关系

	private boolean dbcolumn;// 是否数据库字段 1是 0不是

	private String idcreatetype;// 主键值创建类型 0用户输入 1数据库增长 2UUID 3序列
	public static String IDCREATETYPE_ASSIGNED = "0";
	public static String IDCREATETYPE_INCREMENT = "1";
	public static String IDCREATETYPE_UUID = "2";
	public static String IDCREATETYPE_SEQUENCE = "3";

	private boolean nullable;// 是否允许为空： 1 允许，0 不允许

	private String defaultValue;// 默认值

	private int datalength;// 数据长度

	private int datadecimals;// 小数位

	private boolean rddcolumn;// 是否冗余字段

	private String rddsourcetable;// 冗余字段的来源数据表

	private static String RDDRELCOLUMN_SEQ = ",";
	private String rddrelcolumn;// 冗余字段的关联字段

	private String rddsourcecolumn;// 冗余字段的来源字段

	@EmbeddedId
	public DicColumnPK getDicColumnPK()
	{
		return dicColumnPK;
	}

	public void setDicColumnPK(DicColumnPK dicColumnPK)
	{
		this.dicColumnPK = dicColumnPK;
	}

	@Column(name = "COLUMNNAME", length = 50)
	public String getColumnname()
	{
		return columnname;
	}

	public void setColumnname(String columnname)
	{
		this.columnname = columnname;
	}

	@Column(name = "DATATYPE", length = 50)
	public String getDatatype()
	{
		return datatype;
	}

	public void setDatatype(String datatype)
	{
		this.datatype = datatype;
	}

	@Column(name = "ENUMERATEMAP", length = 255)
	public String getEnumeratemap()
	{
		return enumeratemap;
	}

	public void setEnumeratemap(String enumeratemap)
	{
		this.enumeratemap = enumeratemap;
	}

	@Column(name = "SOURCEELEMENT", length = 50)
	public String getSourceelement()
	{
		return sourceelement;
	}

	public void setSourceelement(String sourceelement)
	{
		this.sourceelement = sourceelement;
	}

	@Column(name = "SOURCETABLE", length = 50)
	public String getSourcetable()
	{
		return sourcetable;
	}

	public void setSourcetable(String sourcetable)
	{
		this.sourcetable = sourcetable;
	}

	@Column(name = "TARGETTABLE", length = 50)
	public String getTargettable()
	{
		return targettable;
	}

	public void setTargettable(String targettable)
	{
		this.targettable = targettable;
	}

	@Column(name = "DBCOLUMN")
	public boolean getDbcolumn()
	{
		return dbcolumn;
	}

	public void setDbcolumn(boolean dbcolumn)
	{
		this.dbcolumn = dbcolumn;
	}

	@Column(name = "IDCREATETYPE")
	public String getIdcreatetype()
	{
		return idcreatetype;
	}

	public void setIdcreatetype(String idcreatetype)
	{
		this.idcreatetype = idcreatetype;
	}

	@Column(name = "NULLABLE")
	public boolean getNullable()
	{
		return nullable;
	}

	public void setNullable(boolean nullable)
	{
		this.nullable = nullable;
	}

	@Column(name = "DEFAULT_VALUE")
	public String getDefaultValue()
	{
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue)
	{
		this.defaultValue = defaultValue;
	}

	@Column(name = "DATALENGTH")
	public int getDatalength()
	{
		return datalength;
	}

	public void setDatalength(int datalength)
	{
		this.datalength = datalength;
	}

	@Column(name = "DATADECIMALS")
	public int getDatadecimals()
	{
		return datadecimals;
	}

	public void setDatadecimals(int datadecimals)
	{
		this.datadecimals = datadecimals;
	}

	@Column(name = "RDDCOLUMN")
	public boolean getRddcolumn()
	{
		return rddcolumn;
	}

	public void setRddcolumn(boolean rddcolumn)
	{
		this.rddcolumn = rddcolumn;
	}

	@Column(name = "RDDSOURCETABLE")
	public String getRddsourcetable()
	{
		return rddsourcetable;
	}

	public void setRddsourcetable(String rddsourcetable)
	{
		this.rddsourcetable = rddsourcetable;
	}

	@Column(name = "RDDRELCOLUMN")
	public String getRddrelcolumn()
	{
		return rddrelcolumn;
	}

	public void setRddrelcolumn(String rddrelcolumn)
	{
		this.rddrelcolumn = rddrelcolumn;
	}

	@Column(name = "RDDSOURCECOLUMN")
	public String getRddsourcecolumn()
	{
		return rddsourcecolumn;
	}

	public void setRddsourcecolumn(String rddsourcecolumn)
	{
		this.rddsourcecolumn = rddsourcecolumn;
	}

	/**
	 * 
	 * Description : 获得字段定义的枚举列表
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	@Transient
	public List<Map> getEnumerate()
	{
		List<Map> result = new ArrayList<Map>();
		if (null != enumeratemap && !"".equals(enumeratemap.trim()))
		{
			StringTokenizer st = new StringTokenizer(enumeratemap,
					ENUMERATE_SEP);
			while (st.hasMoreTokens())
			{
				String keyvalue = st.nextToken();
				int pos = keyvalue.indexOf(":");
				int len = keyvalue.length();
				if (pos > 0 && pos < len)
				{
					String key = keyvalue.substring(0, pos);
					String value = keyvalue.substring(pos + 1, len);
					Map<String, String> enumerate = new HashMap<String, String>();
					enumerate.put(ENUMERATE_KEY_TAG, key);
					enumerate.put(ENUMERATE_VALUE_TAG, value);
					result.add(enumerate);
				}
			}
		}
		return result;
	}

	/**
	 * Description : 根据key获得字段定义中枚举值
	 * 
	 * @param key
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	@Transient
	public String getEnumerateValue(String key)
	{
		String result = null;
		List<Map> enumerates = this.getEnumerate();
		for (Map enumerate : enumerates)
		{
			String enumerateKey = (String) enumerate.get(ENUMERATE_KEY_TAG);
			if (enumerateKey.equals(key))
			{
				result = (String) enumerate.get(ENUMERATE_VALUE_TAG);
				return result;
			}
		}
		return result;
	}

	/**
	 * Description : 解析name属性，得到字段编码列表
	 * 
	 * @return
	 * 
	 */
	@Transient
	public List<String> getRddrelcolumns()
	{
		List<String> result = new ArrayList<String>();
		if (null != rddrelcolumn && !"".equals(rddrelcolumn.trim()))
		{
			StringTokenizer st = new StringTokenizer(rddrelcolumn,
					RDDRELCOLUMN_SEQ);
			while (st.hasMoreTokens())
			{
				String column = st.nextToken();
				if (null != column && !"".equals(column.trim()))
				{
					result.add(column);
				}
			}
		}
		return result;
	}

	/**
	 * Description : 解析name属性，得到首个字段编码
	 * 
	 * @return
	 * 
	 */
	@Transient
	public String getFirstRddrelcolumn()
	{
		List<String> columncodes = getRddrelcolumns();
		if (null == columncodes || columncodes.size() == 0)
		{
			return null;
		}
		return columncodes.get(0);
	}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + dicColumnPK.hashCode();
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final DicColumn other = (DicColumn) obj;
		if (dicColumnPK.equals(other.dicColumnPK))
			return false;
		return true;
	}
}
